package com.cc.controller;


import com.cc.entity.Employees;
import com.cc.entity.R;
import com.cc.enums.HttpEnum;
import com.cc.service.EmployeesService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/employees")
@CrossOrigin
public class EmployeesController {

    @Autowired
    EmployeesService employeesService;

    @PostMapping
    public R save(@Valid Employees employees) {
        int i = employeesService.save(employees);
        if (i == 0) {
            return R.fail(HttpEnum.BAD_REQUEST.getCode(), "新增失败");
        }
        System.out.println(i);
        return R.success(i);
    }

    @DeleteMapping("/{employeeId}")
    public R delete(@PathVariable Integer employeeId) {
        int i = employeesService.delete(employeeId);
        if (i == 0) {
            return R.fail(HttpEnum.BAD_REQUEST.getCode(), "删除失败");
        }
        return R.success("删除成功");
    }

    @PutMapping
    public R update(@RequestBody Employees employees) {
        int i = employeesService.update(employees);
        if (i == 0) {
            return R.fail(HttpEnum.BAD_REQUEST.getCode(), "编辑失败");
        }
        return R.success("编辑成功");
    }

    @GetMapping("/{id}")
    public R queryById(@PathVariable("id") Integer id) {
        Employees employees = employeesService.queryById(id);
        return R.success(employees);
    }


    @GetMapping("/page")
    public R<PageInfo<Employees>> getPage(String empName, String phoneNumber, Integer pageNum) {
        PageInfo<Employees> pageInfo = employeesService.getPage(empName, phoneNumber, pageNum);
        return R.success(pageInfo);
    }
}
